﻿using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace Executors
{
    public class DotNetExecutor : IExecutor
    {
        public List< string > Execute( List< string > inputs, string pathToTheExecutable )
        {
            return inputs.Select( input => ExecuteForInput( pathToTheExecutable, input ) ).ToList( );
        }

        private string ExecuteForInput( string pathToTheExecutable, string input )
        {
            var process = new Process
            {
                StartInfo =
                {
                    FileName = pathToTheExecutable,
                    Arguments = input,
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true
                }
            };
            process.Start();
            process.WaitForExit();

            var err = process.StandardError.ReadToEnd( );
            if ( !string.IsNullOrEmpty( err ) )
            {
                return err;
            }
            else
            {
                return process.StandardOutput.ReadToEnd().Trim();   
            }
        }
    }
}
